#coding utf-8
'''
lizhuangzhuang
2021.0412
'''

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

#数据处理
dataset = pd.read_csv('studentscores.csv')
x = dataset.iloc[:,:1].values
y = dataset.iloc[:,1].values

x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=1/4,random_state=0)

#将数据集进行线性回归拟合
regressor = LinearRegression()
regressor = regressor.fit(x_train,y_train)

#预测结果
Y_pred = regressor.predict(x_test)

#数据可视化
#可视化训练集
plt.scatter(x_train,y_train,c='red')
plt.plot(x_train,regressor.predict(x_train),c='blue')
plt.show()
#可视化测试集
plt.scatter(x_test,y_test,c='red')
plt.plot(x_test,regressor.predict(x_test),c='blue')
plt.show()